package com.atguigu.gulimall.cart.exception;

import com.atguigu.common.utils.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author lk
 * @version 1.0.0
 * @Description 统一异常处理
 * @createTime 2024/3/5 17:48
 */
@ControllerAdvice
public class RuntimeExceptionHandler {

    /**
     * 全局统一异常处理
     * @param exception
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public R handler(RuntimeException exception) {
        return R.error(exception.getMessage());
    }

    @ExceptionHandler(CartException.class)
    public R userHandler(CartException exception) {
        return R.error("购物车无此商品");
    }
}
